package com.boot.filter;


import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.boot.utils.JwtUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //获取JWT
        String token = request.getHeader("token");
        log.info("-----token---->" + token);
        try {
            if (token != null) {
                JwtUtils.tokenVerify(token);
            }
        } catch (SignatureVerificationException e) {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("异常" + e.getMessage());
            return;
            //TODO 有问题自行研究
//            throw new AccessDeniedException("非法token");
        }
        filterChain.doFilter(request, response);
    }

}
